<template>
  <editor-tabs
    :editor-init="editorInit"
    :tabs-remove="handleTabsRemove"
    :tabs-click="changeToolbar"
    :state="$store.state.matlab"
  />
</template>

<script>
import EditorTabs from '@/components/EditorTabs';
import ace from 'brace';
import { getObjectUrl } from '@/utils/aliOss';
import { downloadOss } from '@/api/userProject';

export default {
  components: {
    EditorTabs
  },
  data() {
    return {
      functions: [], // 获取当前函数库下的函数
      func: null
    };
  },
  mounted() {
    this.getFunctions();
  },
  methods: {
    getFunctions() {
      // 获取公共库函数
      getObjectUrl('/public.json').then(publicJsonUrl => {
        downloadOss(publicJsonUrl).then(publicJson => {
          const reader = new FileReader();
          reader.readAsText(publicJson);
          reader.onload = () => {
            this.func = JSON.parse(reader.result);
          };
        });
      });

      // 获取私有库函数
      const this_ = this;
      var functions = [];
      this_.axios.get('/userProject/getFunctionLibrary').then((res) => {
        const prefix = this.$store.getters['user/getUserprefix'];
        const data = res.libraries;
        if (data == null || data.length === 0) {
          return;
        }
        data.forEach((item) => {
          const libName = item.libraryName;
          this_.axios.get('/userProject/getFunctions?libraryName=' + libName).then((res) => {
            const rdata = res.functions;
            if (rdata == null || rdata.length === 0) {
              return;
            }
            rdata.forEach((ritem) => {
              const completeLabel = 'kaitian.' + libName.replace(prefix, '') + '.' + ritem.functionName;
              functions.push({
                meta: 'user func',
                score: 2,
                caption: completeLabel,
                value: completeLabel
              });
            });
          });
        });
        this_.functions = functions;
        localStorage.setItem('functions', JSON.stringify(functions));
      });
    },
    editorInit() {
      require('brace/ext/language_tools');
      require('brace/mode/python');
      require('brace/theme/chrome');
      require('brace/mode/less');
      require('brace/snippets/python');
      const that = this;
      const completer = {
        identifierRegexps: [/[a-zA-Z_0-9\.]/],
        getCompletions: function(editors, session, pos, prefix, callback) {
          that.setCompleteData(editors, session, pos, prefix, callback);
          // that.setCompleteFun(editors, session, pos, prefix, callback);
        }
      };
      const lnTools = ace.acequire('ace/ext/language_tools');
      lnTools.setCompleters([completer, lnTools.snippetCompleter, lnTools.textCompleter, lnTools.keyWordCompleter]);
    },
    setCompleteData(editors, session, pos, prefix, callback) {
      var this_ = this;
      var PublicData = this_.func;
      var PrivateData = this_.functions;
      if (prefix === 'pangu.') {
        return callback(null, PublicData);
      }
      if (prefix === 'kaitian.') {
        return callback(null, PrivateData);
      }
    },
    // setCompleteFun(editors, session, pos, prefix, callback) {
    //   var this_ = this;
    //   var data = this_.func;
    //   if (prefix === 'pangu.') {
    //     return callback(null, data);
    //   }
    // },
    handleTabsRemove(targetName) {
      this.$store.commit('matlab/setEditorFiles', {
        type: 'remove',
        data: {
          id: targetName
        }
      });
      this.changeToolbar();
    },
    changeToolbar() {
      const store = this.$store.state.matlab;
      const index = store.editorFiles.findIndexByKey('id', store.editorFilesId);
      if (index !== -1) {
        this.$store.commit('matlab/changeToolbar', store.editorFiles[index].display === 'file' ? 'editor' : store.editorFiles[index].display);
      }
    }
  }
};
</script>
